package LRU;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 利用hashmap做lru算法缓存
 * Created by luoqifei on 2017/8/22.
 */
public class MyLruCache<K,V> extends LinkedHashMap<K,V> {
    private int capacity;
    public MyLruCache(int capacity){
        super(16,0.75f,true);
        this.capacity = capacity;
    }
    @Override
    public boolean removeEldestEntry(Map.Entry<K,V> eldest) {
        return size() > capacity;
    }

    public static void main(String[] args) {
        MyLruCache<Integer,Integer> cache = new MyLruCache<Integer,Integer>(3);
        cache.put(1,1);
        cache.put(2,3);
        cache.get(1);
        cache.put(11,2);
        cache.put(4,5);
        for(Iterator<Map.Entry<Integer,Integer>>it = cache.entrySet().iterator();it.hasNext();){
            System.out.println(it.next().getKey());
        }
    }
}
